package com.hsc.eduservice.listener;


import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hsc.eduservice.entity.EduSubject;
import com.hsc.eduservice.entity.excel.SubjectData;
import com.hsc.eduservice.service.EduSubjectService;
import com.hsc.servicebase.exceptionHandle.GuliException;

/**
 * easyexcel监听器
 */
public class SubjectExcelListener extends AnalysisEventListener<SubjectData> {

    //这里不能够采用@Autowired注入，因为监听器本身没有交给spring来管理，
    //调用监听器用new的方法构造，把subjectservice传进来，调用service的一些方法(调用数据库)
    public EduSubjectService subjectService;

    public SubjectExcelListener() {
    }

    public SubjectExcelListener(EduSubjectService subjectService) {
        this.subjectService = subjectService;
    }

    /**
     * 一行一行读取excel内容，
     * @param subjectData 读取的数据
     * @param analysisContext
     */
    @Override
    public void invoke(SubjectData subjectData, AnalysisContext analysisContext) {
        if(subjectData==null){
            throw new GuliException(20001,"文件数据为空");
        }
        //一行一行读取excel，目前表中每行有两个值，第一个值为一级分类，第二个值为二级分类
        EduSubject eduOneSubject = this.existOneSubject(subjectService, subjectData.getOneSubjectName());
        if(eduOneSubject==null){//没有相同一级分类可以添加数据
            eduOneSubject = new EduSubject();
            eduOneSubject.setParentId("0"); //定义一级分类，父id为0
            eduOneSubject.setTitle(subjectData.getOneSubjectName()); //获取名称
            subjectService.save(eduOneSubject);//保存到数据库
            // 保存之后eduOneSubject对象会生成id值（原因是MP的id生成策略，实体类中有注解）
        }
        String pid = eduOneSubject.getId();
        EduSubject eduTwoSubject = this.existTwoSubject(subjectService, subjectData.getTwoSubjectName(), pid);
        if(eduTwoSubject==null){//没有相同的二级分类则可以添加数据
            eduTwoSubject = new EduSubject();
            eduTwoSubject.setTitle(subjectData.getTwoSubjectName());
            eduTwoSubject.setParentId(pid);
            subjectService.save(eduTwoSubject);
        }
    }

    /**
     * 判断一级分类不能重复添加
     * @param subjectService 传进service，调用数据库方法
     * @param name 一级分类名称
     * @return
     */
    private EduSubject existOneSubject(EduSubjectService subjectService,String name){
        QueryWrapper<EduSubject> wrapper = new QueryWrapper<>();
        wrapper.eq("title",name);
        wrapper.eq("parent_id",0);
        EduSubject oneSubject = subjectService.getOne(wrapper);
        return oneSubject;
    }

    /**
     * 判断二级分类不能重复添加
     * @param subjectService 传进service，调用数据库方法查询数据
     * @param name 传进来的二级分类名称
     * @param pid
     * @return
     */
    private EduSubject existTwoSubject(EduSubjectService subjectService,
                                       String name,
                                       String pid){
        QueryWrapper<EduSubject> wrapper = new QueryWrapper<>();
        wrapper.eq("title",name).eq("parent_id",pid);
        EduSubject twoSubject = subjectService.getOne(wrapper);
        return twoSubject;
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {

    }
}
